package com.leaves.javamall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.leaves.javamall.product.entity.BrandEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.leaves.javamall.product.entity.CategoryEntity;
import com.leaves.javamall.product.service.CategoryService;
import com.leaves.common.utils.PageUtils;
import com.leaves.common.utils.R;



/**
 * 商品三级分类
 *
 * @author leaves
 * @email lxhu.work@gmail.com
 * @date 2020-08-10 16:47:20
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;



    /**
     * 列表
     */
//    查出所有分类以及子分类，用树形结构组装
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        List<CategoryEntity> Entity = categoryService.listWithTree();

        return R.ok().put("data", Entity);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId/**,@RequestBody CategoryEntity categoryEntity*/){
        System.out.println("使用info修改数据");
        CategoryEntity category = categoryService.getById(catId);
        System.out.println("category name: "+category.getName());
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }


////批量修改
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateDetail(category);
//        categoryService.updateById(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){

//		categoryService.removeByIds(Arrays.asList(catIds));
        //检查当前菜单是否被其他地方引用
		categoryService.removeMuneIds(Arrays.asList(catIds));
        return R.ok();
    }

}
